const http = require('http');
const fs = require('fs');

var hostname = '127.0.0.1';
var port = 3000;

var server = http.createServer((req, res) => {
    res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain;charset=utf-8');
if (req.url !== 'favicon.ico') {
    let out = fs.createWriteStream('./2/log.txt');
    out.write(`请求方法：${req.method}\n`);
    out.write(`请求url：${req.url}\n`);
    out.write(`请求头数据：${JSON.stringify(req.headers, null, 4)}\n`);
    out.write(`请求http版本：${req.httpVersion}\n`);
}
res.write("111\n")
res.end('有点累了！');
})
;

server.listen(port, hostname, () => {
    console.log(`服务器运行在http://${hostname}:${port}`);
})
;

// 关闭事件
// server.close(port,hostname,()=>{
//     console.log('服务器关闭啦！');
// });

server.timeout = 1000;  //设置超时时间为1秒
console.log(server.timeout);